﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsOrderManagement
{
    public partial class AddForm : Form
    {
        public AddForm()
        {
            InitializeComponent();
            LoadData1();
            LoadData2();
        }
        private void LoadData1()
        {    // 清空所有现有列（包括手动添加的）
            dataGridView1.Columns.Clear();

            // 允许自动生成列
            dataGridView1.AutoGenerateColumns = true;

            //把product的List绑定到dataGridView1
            dataGridView1.DataSource = initial.products;

            dataGridView1.Columns["ID"].HeaderText = "商品编号";
            dataGridView1.Columns["NAME"].HeaderText = "商品";
            dataGridView1.Columns["Price"].HeaderText = "商品单价（元/公斤）";

        }

        private void LoadData2()
        {
            // 清空所有现有列（包括手动添加的）
            dataGridView2.Columns.Clear();

            // 允许自动生成列
            dataGridView2.AutoGenerateColumns = true;
            //把customers的List绑定到dataGridView2
            dataGridView2.DataSource = initial.customers;

            dataGridView2.Columns["ID"].HeaderText = "客户编号";
            dataGridView2.Columns["Name"].HeaderText = "名字";
            dataGridView2.Columns["Contact"].HeaderText = "联系方式";
        }

        //确定添加订单
        private async void button1_Click(object sender, EventArgs e)
        {//查看信息是否完整，如果信息不完整或者信息格式不对，出现报错信息
         
            //用户输入的信息
            string s1 = this.textBox1.Text;
            string s2 = this.textBox2.Text;
            string s3 = this.textBox3.Text;


            //s1与s2的检查：
            if( (s1=="1"|| s1 == "2"|| s1 == "3" || s1 == "4")&&(s2=="1"||s2 == "2" || s2== "3"))
            {
                //检查s3
                if (int.TryParse(s3, out int input3))
                {
                    int input1 = int.Parse(this.textBox1.Text);//商品编号
                    int input2 = int.Parse(this.textBox2.Text);//用户编号
                    Product p = initial.products[input1 - 1];
                    Customer c = initial.customers[input2 - 1];
                    initial.OrderID += 1;//订单编号自动累加
                    string oid = initial.OrderID.ToString();
                    Order o = new Order(oid, c, DateTime.Now,(new OrderDetail(p, input3)));


                    initial.orderService.AddOrder(o);

                    //成功标签提示
                    label6.Text = "订单信息添加成功！";
                    //数据清空
                    textBox1.Clear();
                    textBox2.Clear();
                    textBox3.Clear();
                    await Task.Delay(2000);//等两秒
                    label6.Text = "这里是信息提示";
                    
                }
                else
                {
                    AddErrorForm adderroeForm1 = new AddErrorForm();
                    DialogResult result = adderroeForm1.ShowDialog();
                }
            }
            else
            {
                AddErrorForm adderroeForm = new AddErrorForm();
                DialogResult result = adderroeForm.ShowDialog();
            }
        }
        //返回按钮，有问题，待会儿改
        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //取消添加订单,清空
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void label3_Click(object sender, EventArgs e)
        {
           //donothing
        }
    }
}
